﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection.Core;
using System.Threading;

namespace Pixysoft.Framework.Reflection.Controller
{
    class DummyCacheFactory<T>
    {
        private static readonly ReaderWriterLock locker = new ReaderWriterLock();

        List<DummyCacheItem> caches = new List<DummyCacheItem>();

        private static volatile DummyCacheFactory<T> instance;

        private static object syncRoot = new Object();

        public static DummyCacheFactory<T> Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new DummyCacheFactory<T>();
                        }
                    }
                }
                return instance;
            }
        }

        public bool Contains(string nameSpace, string fullName, string name)
        {
            return GetValue(nameSpace, fullName, name) != null;
        }

        public T GetValue(string nameSpace, string fullName, string name)
        {
            locker.AcquireReaderLock(Timeout.Infinite);

            try
            {
                foreach (DummyCacheItem item in caches)
                {
                    if (!string.Equals(nameSpace, item.NameSpace, StringComparison.OrdinalIgnoreCase))
                        continue;

                    if (!string.Equals(fullName, item.FullName, StringComparison.OrdinalIgnoreCase))
                        continue;

                    if (!string.Equals(name, item.Name, StringComparison.OrdinalIgnoreCase))
                        continue;

                    return item.Handler;
                }

                return default(T);
            }
            finally
            {
                locker.ReleaseReaderLock();
            }
        }

        public void AddValue(string nameSpace, string fullName, string name, T value)
        {
            locker.AcquireWriterLock(Timeout.Infinite);

            try
            {
                DummyCacheItem item = new DummyCacheItem();
                item.NameSpace = nameSpace;
                item.FullName = fullName;
                item.Name = name;
                item.Handler = value;
                caches.Add(item);
            }
            finally
            {
                locker.ReleaseWriterLock();
            }
        }

        public void ClearAll()
        {
            locker.AcquireWriterLock(Timeout.Infinite);

            try
            {
                caches.Clear();
            }
            finally
            {
                locker.ReleaseWriterLock();
            }
        }

        class DummyCacheItem
        {
            string nameSpace;
            string fullName;
            string name;
            T handler;


            public string NameSpace
            {
                get { return nameSpace; }
                set { nameSpace = value; }
            }
            public string FullName
            {
                get { return fullName; }
                set { fullName = value; }
            }
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            public T Handler
            {
                get { return handler; }
                set { handler = value; }
            }

        }
    }

}

